package com.lixingye.meowstar.controller;

import com.lixingye.meowstar.common.AppResult;
import com.lixingye.meowstar.common.ResultCode;
import com.lixingye.meowstar.config.AppConfig;
import com.lixingye.meowstar.model.Article;
import com.lixingye.meowstar.model.ArticleReply;
import com.lixingye.meowstar.model.User;
import com.lixingye.meowstar.service.IArticleReplyService;
import com.lixingye.meowstar.service.IArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 猫大仙
 * @since 2025-01-14
 */
@Slf4j
@Tag(name = "帖子回复接口")
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {
    @Autowired
    private IArticleReplyService articleReplyService;
    @Autowired
    private IArticleService articleService;
    @Operation(summary = "回复帖子")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @Parameter(description = "帖子ID") @RequestParam("articleId") @NonNull Long articleId,
                            @Parameter(description = "回复内容") @RequestParam("content") @NonNull String content){
        //获取用户
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //获取帖子
        Article article = articleService.selectbById(articleId);
        if(article==null || article.getDeleteState()==1 || article.getState()==1){
            log.warn(ResultCode.FAILED_ARTICLE_BANNED.toString()+",article id = " +articleId);
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId);
        articleReply.setPostUserId(user.getId());
        articleReply.setContent(content);
        articleReplyService.create(articleReply);
        return AppResult.success();
    }

    @Operation(summary = "获取帖子回复列表")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(
            @Parameter(description = "帖子ID") @RequestParam("articleId") @NonNull Long articleId){
        Article article = articleService.selectbById(articleId);
        if(article==null || article.getDeleteState()==1 || article.getState()==1){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }
        List<ArticleReply> articleReplies = articleReplyService.selectByArticleId(articleId);
        return AppResult.success(articleReplies);
    }
}
